package com.yequan.file.delete.concurrent;

import com.yequan.file.delete.DeleteFileTask;
import com.yequan.thread.threadpool.mythreadpoolmanager.CustomThreadPoolManager;
import com.yequan.thread.threadpool.mythreadpoolmanager.ThreadPoolManagerFactory;
import com.yequan.thread.threadpool.prioritythreadpool.PriorityThreadPoolExecutor;

import java.io.IOException;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.concurrent.TimeUnit;

/**
 * @author yq
 * @date 2020/08/20
 */
public class PriorityThreadPoolDelete {

    public static void main(String[] args) throws IOException {
        String strPath = "E:\\TEMP\\bpast\\2";
        Path path = Paths.get(strPath);

        PriorityThreadPoolExecutor executor = new PriorityThreadPoolExecutor(16, 16, 1, TimeUnit.MINUTES);

        Files.walkFileTree(path, new SimpleFileVisitor<Path>() {

            @Override
            public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
                executor.submit(new PriorityDeleteTask(file), 10);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult postVisitDirectory(Path dir, IOException exc) throws IOException {
                executor.submit(new PriorityDeleteTask(dir), 1);
                return FileVisitResult.CONTINUE;
            }

            @Override
            public FileVisitResult visitFileFailed(Path file, IOException exc) throws IOException {
                return super.visitFileFailed(file, exc);
            }
        });
    }

}
